package com.dy.drp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.drp.entity.Client;
import com.dy.drp.entity.Provider;
import com.dy.drp.service.IClientService;
import com.dy.drp.util.SelectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/client")
@Api(tags="客户管理接口")
public class ClientController {
    @Autowired
    private IClientService clientService;

    @GetMapping("/selectClient/{pageNum}/{pageSize}")
    @ApiOperation("获取所有客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="当前页面"),
            @ApiImplicitParam(name="pageSize",value = "页面展示条数")
    })
    public IPage<Client> selectClient(@PathVariable("pageNum")Integer pageNum,
                                        @PathVariable("pageSize")Integer pageSize){
        Page<Client> page = new Page(pageNum,pageSize);
        return  clientService.page(page);
    }

    @PostMapping("/insertClient")
    @ApiOperation("添加客户")
    public String insertClient(Client client){
        if(clientService.save(client)){
            return "客户添加成功";
        }
        return "客户添加失败";
    }

    @PostMapping("/updateClient")
    @ApiOperation("修改客户信息")
    public String updateClient(Client client){
        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        wrapper.eq("client_name",client.getClientName());
        if(clientService.update(client,wrapper)){
            return "修改成功";
        }
        return "修改失败";
    }

    @GetMapping("/selectOneClient/{clientName}")
    @ApiOperation("查看客户信息")
    @ApiImplicitParam(name="clientName",value = "客户名称")
    public Client selectClientByName(@PathVariable("clientName")String clientName){
        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        wrapper.eq("client_name",clientName);
        return clientService.getOne(wrapper);
    }

    @PostMapping("/selectByNameAndScope")
    @ApiOperation("通过客户名字和经营范围查询客户信息")
    public IPage<Client> selectByNameAndScope(SelectUtil selectUtil) {
        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        wrapper.like("client_name", selectUtil.getName());
        wrapper.like("lient_scop", selectUtil.getStartTime());
        Page<Client> page = new Page<>(selectUtil.getPageNum(),selectUtil.getPageSize());
        return clientService.page(page, wrapper);
    }

    @GetMapping("/selectAllClientNames")
    @ApiOperation("获取所有客户名")
    public List<Client> selectAllClientNames(){
        return clientService.list();
    }
}
